package com.xc.freeapp.account.controller;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.CheckUtils;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.entity.Users;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.redis.VerificationCodeDao;
import com.xc.freeapp.service.UsersService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.CertNoUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.TokenAnnotation;
import com.xc.freeapp.util.UploadUtil;

/**
 * 实现用户信息相关功能
 * 
 * @author wukailuo
 * 
 */
@RestController
@RequestMapping("v2/user")
@Api(value ="/v2user",description= "用户中心")
public class UsersControllerV2 extends AbstractController{
	@Autowired
	private UsersService userService;
	@Autowired
	VerificationCodeDao verificationCodeDaoImpl;
	/**
	 * 用户详情
	 * 
	 * @throws BaseException
	 */
//	@TokenAnnotation(required=true)
//	@RequestMapping(value = "", method = RequestMethod.GET)
//	@ResponseBody
//	@ApiOperation(value = "用户详情")
	public Users getUserInfo() throws BaseException {
		
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		Users users = userService.getUserInfo(authInfo.getUserId());
		users.setLoginpwd("");
		users.setIcon(UploadUtil.urlConversion(users.getIcon()));
		return users;
	}

	/**
	 * 用户详情修改
	 * 
	 * @throws BaseException
	 */
//	@TokenAnnotation(required=true)
//	@RequestMapping(value = "", method = RequestMethod.PUT)
//	@ResponseBody
//	@ApiOperation(value = "用户详情修改")
	public Users updateUserInfo(@RequestBody Users user)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		if(user.getRealName()==null||"".equals(user.getRealName())){
			throw new BaseException(Constant.ERRORCODE_558);
		}
		user.setUserid(authInfo.getUserIntId());
		user.setIcon(UploadUtil.upload(user.getIcon(),authInfo.getAppIntId()));
		if(user!=null&&user.getCardId()!=null&&user.getCardId()!=""){
			if(!CertNoUtil.vId(user.getCardId())){
				throw new BaseException(Constant.ERRORCODE_556);
			}
		}
		userService.updateuserinfo(user);
		
		Users users = userService.getUserInfo(authInfo.getUserId());
		users.setIcon(UploadUtil.urlConversion(users.getIcon()));
		return users;
	}

	/**
	 * 更新用户信息接口
	 * 
	 * @param request
	 * @return
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "/userinfo",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "更新用户信息接口")
	public ResponseBean updateUserInfo(@RequestParam("oldvalue") String oldvalue,
			@RequestParam("updatetype") String updatetype,
			@RequestParam("updatevalue") String updatevalue,
			@RequestParam("verificationCode") String verificationCode)
			throws BaseException {
		String telephone = "";
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		try {
			
			
			Map<String, String> map = new HashMap<String, String>();
			
			Users users = userService.getUserInfo(authInfo.getUserId());
			String verCode = verificationCodeDaoImpl.get(users.getLoginname());
			log.info("redis中验证码为verCode=" + verCode);
			if (verCode == null) {
				throw new BaseException("151", "验证码错误");
			}
			if (!verCode.equals(verificationCode)) {
				throw new BaseException("151", "验证码错误");
			}
			users.setIcon(UploadUtil.urlConversion(users.getIcon()));
			if (users.getLoginpwd()!=null&&users.getLoginpwd().equals(oldvalue)) {
				map.put("userId", authInfo.getUserId());
				
				CheckUtils.matchPattern(Constant.VER_NOTNULL, updatetype,
						"^[1,3]");
				map.put("updatetype", updatetype);
				map.put("updatevalue", updatevalue);
				userService.updateUserInfo(map);
				return ResponseBean.getSuccess();
			} else {
				throw new BaseException(Constant.PWD_ERROR);
			}
		} catch (BaseException e) {
			log.error(telephone + ":更新用户信息出错" + e.getMessage());
			throw e;
		} catch (Exception e) {
			log.error(telephone + ":更新用户信息出错" + e.getMessage());
			throw new BaseException(Constant.GLOBAL_ERROR);
		}
	}
	
	/**
	 * 用户qq,weixin修改前验证是否被占用
	 * @param type
	 * @param _val
	 * @return
	 * @throws BaseException
	 */
//	@TokenAnnotation(required=true)
//	@RequestMapping(value = "/beforeUpdateqqorweixin",method = RequestMethod.GET)
//	@ResponseBody
//	@ApiOperation(value = "qq号、微信号占用验证")
	public ResponseBean beforeUpdateqqorweixin(@RequestParam("type") String type,@RequestParam("_val") String _val)throws BaseException{
		 AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		 Map<String, String>paramsmap = new HashMap<String, String>();
		 paramsmap.put("userid", authInfo.getUserId());
		 paramsmap.put("type", type);
		 paramsmap.put("_val", _val);
		 int a = userService.selectCount(paramsmap);
		 if(a>0){
			 throw new BaseException(Constant.QQ_ERROR);
		 }else{
			 return ResponseBean.getSuccess();
		 }
	}
}
